package com.ruoyi.project.drug.service.impl;

import com.ruoyi.project.drug.domain.DrugOrder;
import com.ruoyi.project.drug.domain.DrugOrderCustom;
import com.ruoyi.project.drug.domain.DrugOrderDetail;
import com.ruoyi.project.drug.mapper.DrugOrderCustomMapper;
import com.ruoyi.project.drug.service.IDrugOrderCustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname DrugOrderCustomServiceImpl
 * @Description
 * @Date 2022/6/28 23:04
 * @Author Lee
 */
@Service
public class DrugOrderCustomServiceImpl implements IDrugOrderCustomService {

    @Autowired
    private DrugOrderCustomMapper customMapper;
    @Override
    public List<DrugOrderCustom> selectDrugOrderCustomList(DrugOrderCustom drugOrderCustom) {
        return customMapper.selectDrugOrderCustomList(drugOrderCustom);
    }

    @Override
    public int insertDrugOrderAndDetails(Integer supportId, Double orderTotal, String createBy, List<DrugOrderDetail> orderDetails) {
        // 创建采购单据对象
        DrugOrder order = new DrugOrder();
        order.setOrderMan(createBy);
        order.setOrderSupId(supportId);
        order.setOrderTotal(orderTotal);
        order.setCreateBy(createBy);

        // 新增采购单据
        customMapper.insertDrugOrder(order);

        // 循环新增采购单据明细
        for(DrugOrderDetail drugOrderDetail : orderDetails){
            // 把订单主键设置到明细的外键字段中
            drugOrderDetail.setOrderId(order.getOrderId());
            // 新增到数据库
            customMapper.insertDrugOrderDetail(drugOrderDetail);
        }

        return 1;
    }


}
